<?php

namespace common\models\datas;

use Yii;
use common\components\extenders\MenuManage;
use frontend\models\datas\Mechanic;

/**
 * This is the model class for table "{{%balance}}".
 *
 * @property integer $balance_id
 * @property string $title
 * @property integer $mechanic_id
 * @property string $price
 * @property integer $state
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $voucher
 * @property string $type
 */
class Balance extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%balance}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['balance_id'], 'required'],
            [['balance_id', 'mechanic_id', 'state', 'user_id', 'created_at', 'updated_at','site_id'], 'integer'],
            [['price'], 'number'],
            [['title', 'voucher'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'balance_id' => Yii::t('common', 'Balance ID'),
            'title' => Yii::t('common', '结算标题'),
            'mechanic_id' => Yii::t('common', '机构编号'),
            'price' => Yii::t('common', '结算金额'),
            'state' => Yii::t('common', '结算状态'),
            'user_id' => Yii::t('common', '对应课次'),
            'created_at' => Yii::t('common', '上课日期'),
            'updated_at' => Yii::t('common', '修改时间'),
            'voucher' => Yii::t('common', '付款凭证'),
            'type' => Yii::t('common', '结算项目'),
            'site_id' =>Yii::t('common','分店id'),
        ];
    }
    public function fields(){
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }
    public function extraFields()
    {
        return [
            'mechanic_name' => function () {
                return !empty($this->mechanic->name) ? $this->mechanic->name : '';
            },
            'status' => function () {
                return ($this->state == 0) ? '未付款' : '已付款';
            },
            'month' => function () {
                return Yii::$app->formatter->asDate($this->created_at, 'php:m') . '月';
            },
            'name' => function () {
                return $this->admin->name;
            },
            'operate_admission' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'delete' => function ($data) {
                            if ($data['state'] == 1) {
                                return '<button class="yx-btn-disabled" type="button">删除</button>';
                            } else {
                                return '';
                            }
                        },
                        'state' => function ($data) {
                            if ($data['state'] == 1) {
                                return '<button class="yx-btn-disabled" type="button">结算</button>';
                            } else {
                                return '';
                            }
                        },
                        'view' => function ($data) {
                            return '';
                        }
                    ]
                )->create();
            },
            'operate_classhour' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'delete' => function ($data) {
                            if ($data['state'] == 1) {
                                return '<button class="yx-btn-disabled" type="button">删除</button>';
                            } else {
                                return '';
                            }
                        },
                        'state' => function ($data) {
                            if ($data['state'] == 1) {
                                return '<button class="yx-btn-disabled" type="button">结算</button>';
                            } else {
                                return '';
                            }
                        },
                    ]
                )->create();
            },
        ];
    }

    public function getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanic_id']);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }


}
